package com.dongdongsongcan.app.fragment;

import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.SMSReceiver;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AuthnumData;
import com.dongdongsongcan.app.control.bean.UserData;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.AppThread;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.fragment.BaseFragment;

public class LoginPhoneFragment extends BaseFragment {
    private EditText mLogin_et_phonenum;
    private Button mLogin_btn_getverifycode;
    private EditText mLogin_et_verifycode;
    private Button mLogin_btn_phone_loginbtn;
    private AuthnumData mAuthnumData;
    private String mUser_mobile;
    private SMSReceiver mSmsReceiver = new SMSReceiver(new SMSReceiver.ISMSListener() {
        @Override
        public void onSmsReceive(String verifyCode) {
            if (mLogin_et_verifycode == null) return;
            mLogin_et_verifycode.setText(verifyCode);
        }
    });


    @Override
    public void onStart() {
        getActivity().registerReceiver(mSmsReceiver, new IntentFilter(SMSReceiver.SMS_RECEIVED_ACTION));
        super.onStart();
    }

    @Override
    public void onStop() {
        getActivity().unregisterReceiver(mSmsReceiver);
        super.onStop();
    }

    @Override
    public void initView() {
        mLogin_et_verifycode = (EditText) getView().findViewById(
                R.id.login_et_verifycode);
        mLogin_et_phonenum = (EditText) getView().findViewById(
                R.id.login_et_phonenum);
        mLogin_btn_getverifycode = (Button) getView().findViewById(
                R.id.login_btn_getverifycode);
        mLogin_btn_phone_loginbtn = (Button) getView().findViewById(
                R.id.login_btn_phone_loginbtn);

    }

    @Override
    public void setListener() {
        mLogin_btn_phone_loginbtn.setOnClickListener(this);
        mLogin_btn_getverifycode.setOnClickListener(this);
    }

    int tineout = 60;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn_getverifycode:
                mLogin_btn_getverifycode.setText(tineout + "秒");
                mLogin_btn_getverifycode.setEnabled(false);
                AppThread.uiPost(new Runnable() {
                    @Override
                    public void run() {
                        tineout--;
                        mLogin_btn_getverifycode.setText(tineout + "秒");
                        if (tineout <= 0) {
                            mLogin_btn_getverifycode.setEnabled(true);
                            mLogin_btn_getverifycode.setText("重新获取");
                            tineout = 60;
                        } else {
                            AppThread.uiPost(this, 1000);
                        }
                    }
                }, 1000);
                mUser_mobile = mLogin_et_phonenum.getText().toString().trim();
                UserControl.i().authnum(mUser_mobile,
                        new IControlResponse<AuthnumData>() {

                            @Override
                            public void onControlResponse(AuthnumData data) {
                                TipsUtil.toast(
                                        data.getMsg());
                                if (data.getError() == 0) {
                                    mAuthnumData = data;
                                }
                            }
                        });
                break;
            case R.id.login_btn_phone_loginbtn:
                if (mAuthnumData != null) {
                    String verifyCode = mLogin_et_verifycode.getText().toString()
                            .trim();
                    if (!TextUtils.equals(verifyCode, mAuthnumData.getAuthnum())) {// 这里有大问题,
                        // 可能导致批量注册用户!
                        TipsUtil.toast("验证码错误!");
                        return;
                    }

                    UserControl.i().mobile_login(
                            mUser_mobile, // 这里可以统一为一个接口!
                            mAuthnumData.getAuthnum(),
                            mAuthnumData.getSession_id(),
                            new IControlResponse<UserData>() {

                                @Override
                                public void onControlResponse(UserData data) {
                                    if (data.getError() == 0) {
                                        UserControl.i().saveUser(data.getResult());
                                        //MainActivity.startFrom(getActivity());
                                        getActivity().finish();
                                    }
                                    TipsUtil.toast(data.getMsg());

                                }
                            });
                } else {
                    TipsUtil.toast("请先获取验证码");
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void initData() {

    }

    @Override
    protected int onSetContentView() {
        return R.layout.fragment_login_phone;
    }

}
